package com.liziguo.lzgfp.client;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;

/**
 * 解析java client.java -h127.0.0.1 -p 9000 --autoReconnect --reconnect=30
 *
 * @author Liziguo
 * @date 2022/1/5 18:35
 */
public class Options {

    private final ArrayList<String> list = new ArrayList<>();
    private final HashMap<String, String> map = new LinkedHashMap<>();

    public Options(String[] args) {
        int length = args.length;
        if (length == 0) {
            return;
        }
        int i = 0;
        while (i < length) {
            String option = args[i];
            int len;
            if ((len = option.length()) >= 2) {
                if (option.charAt(0) == '-') {
                    if (option.charAt(1) != '-') {
                        // java client.java -h localhost 或 java client.java -hlocalhost
                        if (len == 2) {
                            //java client.java -h localhost
                            if (i + 1 < length) {
                                // 若下一参数是-开头的话 不会当成value (java client.java -p -v)
                                String nextArg = args[i + 1];
                                if (nextArg.charAt(0) != '-') {
                                    i++;
                                    map.putIfAbsent(option, nextArg);
                                } else {
                                    map.putIfAbsent(option, null);
                                }
                            } else {
                                map.putIfAbsent(option, null);
                            }
                        } else {
                            // java client.java -hlocalhost
                            map.putIfAbsent(option.substring(0, 2), option.substring(2));
                        }
                    } else {
                        // java client.java --host=localhost
                        int indexOf = option.indexOf('=');
                        if (indexOf >= 0) {
                            // java client.java --host=localhost
                            map.putIfAbsent(option.substring(0, indexOf), option.substring(indexOf + 1));
                        } else {
                            map.putIfAbsent(option, null);
                        }
                    }
                    i++;
                    continue;
                }
            }
            i++;
            list.add(option);
        }
    }

    public int getInt(String key) {
        try {
            return Integer.parseInt(map.get(key));
        } catch (NumberFormatException e) {
            throw new NumberFormatException("参数" + key + ":" + map.get(key) + "无法转换成int类型数据");
        }
    }

    public int getInt(String key, int defaultValue) {
        try {
            return Integer.parseInt(map.get(key));
        } catch (NumberFormatException e) {
            return defaultValue;
        }
    }

    public int getInt(int index, int defaultValue) {
        if (index < list.size()) {
            try {
                return Integer.parseInt(list.get(index));
            } catch (NumberFormatException ignored) {
            }
        }
        return defaultValue;
    }

    public String getString(String key) {
        return map.get(key);
    }

    public String getString(String key, String defaultValue) {
        String value = map.get(key);
        if (value != null) {
            return value;
        }
        return defaultValue;
    }

    public String getString(int index) {
        if (index < list.size()) {
            return list.get(index);
        }
        return null;
    }

    public String getString(int index, String defaultValue) {
        if (index < list.size()) {
            return list.get(index);
        }
        return defaultValue;
    }

    public boolean contains(String key) {
        return map.containsKey(key);
    }

    @Override
    public String toString() {
        return "Options: " + list + " " + map;
    }
}
